package test.aat.yanfa;

import java.util.StringTokenizer;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

public class StringQiefen {

	public static void main(String[] args) {
		String ip = "192.168.1.168.asd.wrw4.1231e3rq34.dfet45t.2312er.443";
		long st1 = System.nanoTime();
		String[] ips = ip.split("\\.");
		System.out.println("使用jdk的split切分字符串");
		for (int i = 0; i < ips.length; i++) {
			System.out.print(ips[i] + "  ");
		}
		System.out.println("花费时间" + (System.nanoTime() - st1));

		long st2 = System.nanoTime();
		ips = StringUtils.split(ip, "\\.");
		System.out.println("使用common的split切分字符串");
		for (int i = 0; i < ips.length; i++) {
			System.out.print(ips[i] + "  ");
		}
		System.out.println("花费时间" + (System.nanoTime() - st2));

		long st3 = System.nanoTime();
		System.out.println("使用StringTokenizer的切分字符串");
		StringTokenizer token = new StringTokenizer(ip, ".");
		while (token.hasMoreElements()) {
			System.out.print(token.nextToken() + "  ");
		}
		System.out.println("花费时间" + (System.nanoTime() - st3));

		long st4 = System.nanoTime();
		Pattern pattern = Pattern.compile("\\.");
		ips = pattern.split(ip);
		System.out.println("使用jdk的pattern切分字符串");
		for (int i = 0; i < ips.length; i++) {
			System.out.print(ips[i] + "  ");
		}
		System.out.println("花费时间" + (System.nanoTime() - st4));

		long st5 = System.nanoTime();
		System.out.println("使用jdk的indexOf切分字符串");
		int k = 0, count = 0;
		for (int i = 0; i < ip.length(); i++) {
			if (ip.substring(i, i + 1).equals(".")) {
				if (count == 0) {
					System.out.print(ip.substring(0, i) + "  ");
				} else if (count == 1) {
					System.out.print(ip.substring(k + 1, i) + "  ");
				} else {
					System.out.print(ip.substring(k + 1, i) + "  ");
					System.out.print(ip.substring(i + 1, ip.length()) + "  ");
				}
				k = i;
				count++;
			}
		}
		System.out.println("花费时间" + (System.nanoTime() - st5));

	}
}
